package com.mby.service.impl;

import com.mby.entity.ChatGroup;
import com.mby.entity.DeviceToken;
import com.mby.entity.Push;
import com.mby.service.IPushService;
import com.mby.utils.CsAppUtil;
import com.mby.utils.MbyPush;
import com.mby.utils.MongoTools;
import com.mby.utils.WebConstant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by developer_01 on 2018/6/11.
 */
@Service("pushService")
public class PushService implements IPushService{

    @Resource
    public MongoTools mongoTool;

    @Override
    public void sentStatusPush(ChatGroup cg) throws Exception{
        DeviceToken dt = CsAppUtil.getDeviceToken(cg.getCustomerId(),cg.getBrandId());
        if(dt != null){
            Push push = new Push();
            push.setGroupId(cg.getId());
            push.setDeviceToken(dt.getDeviceToken());
            push.setPlatform(dt.getPlatform());
            push.setTitle(WebConstant.CS_VEHICLE_STATUS_TITLE);
            push.setContent(WebConstant.CS_VEHICLE_STATUS_CONTENT);
            push.setBrandId(cg.getBrandId());
            push.setCustom(cg.getPushCustom());
            //todo 需要用MQ处理
            MbyPush.sentOnePushToCsApp(push);
        }
    }

    @Override
    public void sentEst15Push(ChatGroup cg) throws Exception{
        DeviceToken dt = mongoTool.findOne("","{'userId':"+cg.getCreatedBy()+"}",DeviceToken.class);
        if(dt != null){
            Push push = new Push();
            push.setGroupId(cg.getId());
            push.setDeviceToken(dt.getDeviceToken());
            push.setPlatform(dt.getPlatform());
            push.setTitle(WebConstant.CS_VEHICLE_EST15_TITLE);
            push.setContent(WebConstant.CS_VEHICLE_EST15_CONTENT);
            push.setCustom(cg.getPushCustom());
            //todo 需要用MQ处理
            MbyPush.sentOnePushToLive(push);
        }
    }

    @Override
    public void sendCSWelPush(Integer groupId) throws Exception{
        ChatGroup chatGroup = mongoTool.findOne("","{'id':"+groupId+"}",ChatGroup.class);
        if(chatGroup != null){
            DeviceToken dt; Push push;
            dt = CsAppUtil.getDeviceToken(chatGroup.getCustomerId(),chatGroup.getBrandId());
            if(dt != null){
                push = new Push();
                push.setGroupId(groupId);
                push.setDeviceToken(dt.getDeviceToken());
                push.setPlatform(dt.getPlatform());
                push.setTitle(WebConstant.CS_LIVE_START_TITLE);
                push.setContent(WebConstant.CS_LIVE_START_CONTENT);
                push.setBrandId(chatGroup.getBrandId());
                push.setCustom(chatGroup.getPushCustom());
                //todo 需要用MQ处理
                MbyPush.sentOnePushToCsApp(push);
            }
        }
    }

    @Override
    public void sendCSOverPush(Integer groupId) throws Exception{
        ChatGroup chatGroup = mongoTool.findOne("","{'id':"+groupId+"}",ChatGroup.class);
        if(chatGroup != null){
            DeviceToken dt; Push push;
            dt = CsAppUtil.getDeviceToken(chatGroup.getCustomerId(),chatGroup.getBrandId());
            if(dt != null) {
                push = new Push();
                push.setGroupId(groupId);
                push.setDeviceToken(dt.getDeviceToken());
                push.setPlatform(dt.getPlatform());
                push.setTitle(WebConstant.CS_LIVE_END_TITLE);
                push.setContent(WebConstant.CS_LIVE_END_CONTENT);
                push.setBrandId(chatGroup.getBrandId());
                push.setCustom(chatGroup.getPushCustom());
                //todo 需要用MQ处理
                MbyPush.sentOnePushToCsApp(push);
            }
        }
    }
}
